package com.kgw.gatewaybluetooth.utils;

import java.util.Collections;
import java.util.Comparator;
import java.util.List;

import android.hardware.Camera;
import android.hardware.Camera.Size;
import android.util.Log;

public class CamParaUtil {
    private static final String TAG = "jayden";
    private CameraSizeComparator sizeComparator = new CameraSizeComparator();
    private static CamParaUtil myCamPara = null;

    private CamParaUtil() {

    }

    public static CamParaUtil getInstance() {
        if (myCamPara == null) {
            myCamPara = new CamParaUtil();
            return myCamPara;
        } else {
            return myCamPara;
        }
    }

    public Size getPropPreviewSize(List<Size> list, float th, int minWidth) {
        Collections.sort(list, sizeComparator);

//		int i = 0;
//		for(Size s:list){
//			if((s.width >= minWidth) && equalRate(s, th)){
//				Log.i(TAG, "PreviewSize:w = " + s.width + "h = " + s.height);
//				break;
//			}
//			i++;
//		}
//		if(i == list.size()){
//			i = 0;
//		}
//		return list.get(i);
        Size currentSize = null;
        int maxWidth = 0;
        for (Size s : list) {
            if (s.width > maxWidth) {
                maxWidth = s.width;
                currentSize = s;
            }
        }

        return currentSize;
    }

    public Size getPropPictureSize(List<Size> list, float th, int minWidth) {
//        Collections.sort(list, sizeComparator);
//
//        int i = 0;
//        for (Size s : list) {
//            if ((s.width >= minWidth) && equalRate(s, th)) {
//                Log.i(TAG, "PictureSize : w = " + s.width + "h = " + s.height);
//                break;
//            }
//            i++;
//        }
//        if (i == list.size()) {
//            i = 0;
//        }
        Size currentSize = null;
        int maxWidth = 0;
        for (Size s : list) {
            if (s.width > maxWidth) {
                maxWidth = s.width;
                currentSize = s;
            }
        }
        return currentSize;
    }

    public boolean equalRate(Size s, float rate) {
        float r = (float) (s.width) / (float) (s.height);
        if (Math.abs(r - rate) <= 0.03) {
            return true;
        } else {
            return false;
        }
    }

    public class CameraSizeComparator implements Comparator<Size> {
        public int compare(Size lhs, Size rhs) {
            // TODO Auto-generated method stub
            if (lhs.width == rhs.width) {
                return 0;
            } else if (lhs.width > rhs.width) {
                return 1;
            } else {
                return -1;
            }
        }

    }


    public void printSupportPreviewSize(Camera.Parameters params) {
        List<Size> previewSizes = params.getSupportedPreviewSizes();
        for (int i = 0; i < previewSizes.size(); i++) {
            Size size = previewSizes.get(i);
            Log.i(TAG, "previewSizes:width = " + size.width + " height = " + size.height);
        }

    }


    public void printSupportPictureSize(Camera.Parameters params) {
        List<Size> pictureSizes = params.getSupportedPictureSizes();
        for (int i = 0; i < pictureSizes.size(); i++) {
            Size size = pictureSizes.get(i);
            Log.i(TAG, "pictureSizes:width = " + size.width
                    + " height = " + size.height);
        }
    }

    public void printSupportFocusMode(Camera.Parameters params) {
        List<String> focusModes = params.getSupportedFocusModes();
        for (String mode : focusModes) {
            Log.i(TAG, "focusModes--" + mode);
        }
    }
}
